home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- ATsis -- create SIRDS out of depth information
- (C) 2/1995 by Christian Perle
- based on sirds program by W.A. Steer
- *************************************************************************/
-
- #include <stdio.h>
-
- #ifndef SEEK_SET
- #define SEEK_SET 0 /* from beginning of file */
- #define SEEK_CUR 1 /* from current location */
- #define SEEK_END 2 /* from end of file */
- #endif
-
- #define RNDMASK 0x7FFF
- #define RNDDIVISOR (float) RNDMASK
-
- #define SI_NAME "sirds.pbm"
-
- /* depth of background in pixels */
- #define BKDEPTH -800
- /* eye separation in pixels */
- #define E 180
- /* observer-screen distance in pixels */
- #define O 700
- /* slow hidden point removal on/off */
- #define DOHIDDENREM 0
- /* width of picture in pixels */
- #define XRES 640
- /* height of picture in pixels */
- #define YRES 470
-
- float rnd(void);
-
- unsigned char thedepth[XRES*YRES];
- unsigned char pixels[XRES];
- unsigned char thesird[XRES/8*(YRES+10)]; /* add 10 "help lines" */
- int z[XRES];
- int link[XRES];
-
- static int pw2[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
-
- FILE *fd;
-
- /* read depth file */
- read_depth(name)
- char *name;
- {
- int x;
-
- fd = fopen(name, "rb");
- if (fd == NULL) {
- fprintf(stderr, "can't open %s\n", name);
- exit(0);
- }
- for (x = 0; x < XRES*YRES; x++) thedepth[x] = fgetc(fd);
- fclose(fd);
- }
-
- /* add help marks */
- init_sirds()
- {
- int x, y, th;
- float helpdist;
-
- /* clear thesird[] array */
- for (x = 0; x < XRES/8*(YRES+10); x++)
- thesird[x] = 0;
-
- helpdist = abs(BKDEPTH)*E/(2.0*(abs(BKDEPTH)+O));
- th = 0;
- for (y = 9; y >= 0; y--) {
- /* left mark */
- for (x = XRES/2-helpdist-th; x <= XRES/2-helpdist+th; x++) {
- thesird[y*XRES/8+x/8] |= pw2[x % 8];
- }
- /* right mark */
- for (x = XRES/2+helpdist-th; x <= XRES/2+helpdist+th; x++) {
- thesird[y*XRES/8+x/8] |= pw2[x % 8];
- }
- th++;
- }
- }
-
- /* render the SIRDS */
- do_sirds()
- {
- int x, y, h, u, dx, highest, separation, left, right;
- int c, visible;
- float v;
-
- for (y = 0; y < YRES; y++) {
- fprintf(stderr, "Rendering line %4d of %4d\r", y+1, YRES);
- for (x = 0; x < XRES*1; x++) link[x] = x;
- highest = BKDEPTH;
- for (x = 0; x < XRES; x++) {
- h = BKDEPTH + thedepth[y*XRES+x];
- z[x] = h;
- if (h > highest) highest = h;
- }
- for (x = 0; x < XRES*1; x++) {
- separation = (E*1*z[x/1])/(z[x/1]-O);
- left = x-separation/2;
- right = left+separation;
- if ((left >= 0) && (right < XRES*1)) {
- visible = 1;
- if (DOHIDDENREM == 1) {
- v = 2.0*(O-z[x/1])/E;
- dx = 1;
- do {
- u = z[x/1]+dx*v;
- if ((z[(x+dx)/1] >= u) || (z[(x-dx)/1] >= u))
- visible = 0;
- dx++;
- } while ((u <= highest) && (visible == 1));
- }
- if (visible == 1) link[right] = left;
- }
- }
- for (x = 0; x < XRES*1; x++) {
- if (link[x] == x) {
- pixels[x] = (int)2*rnd();
- }
- else {
- pixels[x] = pixels[link[x]];
- }
- }
- for (x = 0; x < XRES; x++)
- if (pixels[x] == 1) thesird[(y+10)*XRES/8+x/8] |= pw2[x % 8];
- }
- }
-
- /* write SIRDS as PBM file */
- write_sirds_pbm()
- {
- int x;
-
- fd = fopen(SI_NAME, "wb");
- fprintf(fd, "P4\n");
- fprintf(fd, "%d %d\n", XRES, YRES+10);
- for (x = 0; x < XRES/8*(YRES+10); x++) fputc(thesird[x],fd);
- fclose(fd);
- }
-
- /* return random value between 0 and 1 */
- float rnd(void)
- {
- float z;
-
- z = (float) (rand() & RNDMASK) / (RNDDIVISOR+1);
- return(z);
- }
-
- /* --------------------------------------------------------------------- */
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int argn;
- char *de_name;
-
- fprintf(stderr, "ATsirds v1.0 (C) 2/1995 Christian Perle\n");
- fprintf(stderr, "based on sirds program by W.A. Steer\n\n");
- argn = 1; /* number of arguments */
- if (argn < argc) {
- de_name = argv[argn];
- argn++;
- }
- else { /* too few arguments */
- fprintf(stderr, "usage: atsirds depthfile\n");
- exit(0);
- }
- if (argn != argc) { /* too much arguments */
- fprintf(stderr, "usage: atsirds depthfile\n");
- exit(0);
- }
- fprintf(stderr, "Rendering %s to ", de_name);
- fprintf(stderr, SI_NAME);
- fprintf(stderr, "\n");
- fprintf(stderr, "Size %dx%d", XRES, YRES+10);
- if (DOHIDDENREM == 1) fprintf(stderr, ", hidden point removal.\n\n");
- else fprintf(stderr, ".\n\n");
- read_depth(de_name);
- init_sirds();
- do_sirds();
- fprintf(stderr, "\n");
- write_sirds_pbm();
- fprintf(stderr, "Done.\n");
- }
-